import logging

import pytest

from resource.pom.music import Music
from utils.manager import Images


class Test:
    app = Music

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1：点击音乐应用')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.stop_permission()
        logging.info('预期结果1.正常进入音乐应用内')
        device.assert_current_window(self.app.window_name)

        logging.info('步骤2.正在播放的音乐点击播放键')
        device.refresh_layout()
        play = device.get_element_by_key('image3')

        x1, y1, x2, y2 = device.get_element_area(play)
        crop_area = (x1 + 50, y1 + 50, x2 - 50, y2 - 50)
        device.click_element(play)
        device.wait(3)
        device.click_element(play)
        device.wait(4)
        logging.info('预期结果2.播放键展示为暂停状态')
        pause0_pic = device.save_snapshot_to_local('pause0.jpeg')
        Images.crop_picture(pause0_pic, *crop_area)
        similarity0 = Images.compare_image_similarity(pause0_pic, self.app.standard_pause_pic)
        assert similarity0 > 0.6, '暂停状态的截图对比和标准图相似度过低'
        # assert not device.is_audio_running(), '音乐未停止'

        logging.info('步骤3.暂停状态的音乐点击播放键')
        device.click_element(play)
        device.wait(4)
        logging.info('预期结果3.播放键展示为启动状态')
        running_pic = device.save_snapshot_to_local('running.jpeg')
        Images.crop_picture(running_pic, *crop_area)
        similarity1 = Images.compare_image_similarity(running_pic, self.app.standard_running_pic)
        assert similarity1 > 0.6, '播放状态的截图对比和标准图相似度过低'
        # assert device.is_audio_running(), '音乐没有声音输出'

        logging.info('步骤4.播放完整首音乐后查看播放键状态')
        device.swipe(200, 979, 650, 979)
        device.wait(5)
        logging.info('预期结果4.播放键展示为暂停状态')
        pause1_pic = device.save_snapshot_to_local('pause1.jpeg')
        Images.crop_picture(pause1_pic, *crop_area)
        similarity2 = Images.compare_image_similarity(pause1_pic, self.app.standard_pause_pic)
        assert similarity2 > 0.6, '暂停状态的截图对比和标准图相似度过低'
        # assert not device.is_audio_running(), '音乐未停止'
